
let defaultTitles = [{ "title": "国内", "type": "gn" },
                     { "title": "国际", "type": "gj" },
                     { "title": "财经", "type": "cj" },
                     { "title": "娱乐", "type": "yl" },
                     { "title": "军事", "type": "js" },
                     { "title": "体育", "type": "ty" },
                     { "title": "其他", "type": "other" }]

Page({
  data: {
    titles:defaultTitles,
    curIndex:0,
    newsImageUrl:null,
    newsimageTitle:"",
    newsImageTime:"",
    newsId:null,
    newList:{}
  },
  onLoad: function(option){
    this.getNews(this.data.titles[this.data.curIndex]["type"])
  },
  titlesTap: function(tap){
    let index = tap.currentTarget.dataset.index
    if (this.data.curIndex == index) {
      return
    }
    this.setData({ curIndex: index })
    this.getCurSelectNews()
  },
  getCurSelectNews(completeCallBack){//get curentSelectTitlesData
     
    let selectType = this.data.titles[this.data.curIndex]["type"]
    this.getNews(selectType, completeCallBack)
  }
  ,
  getNews: function(titleType,completeCallBack){
    var that = this
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: 'https://test-miniprogram.com/api/news/list',
      data:{
        "type":titleType
      },
      success: function(res){
        console.log(res)
        if(res.data.code != 200){
          wx.showToast({
            title: res.data.message,
            icon: "none"
          })
          return
        }

        let result = res.data.result
        //formate time
        for(var i =0;i<result.length;i++){
          result[i]["formateTip"] = that.formateSourceAndCity(result[i]["source"], result[i]["date"])
          if (result[i]["firstImage"].length == 0){
            result[i]["firstImage"] = "/pages/images/default.png"
          }
        }
        //hot news
        let hotNews = result.shift()
        //setdata
        if(result.length > 0){
            that.setData({
              "newsImageUrl": hotNews["firstImage"],
              "newsimageTitle": hotNews["title"],
              "newsImageTime": hotNews["formateTip"],
              "newsId":hotNews["id"],
              "newList":result
            })
        }


      },
      complete: function(){
        completeCallBack && completeCallBack()
        wx.hideLoading()
      }
    })

  },
  formateSourceAndCity(source,time){
    if(source == null || source.length == 0) {
      source = "默认新闻"
    }
    let date = new Date(time)
    return source + "      " + date.getHours() + ":" + date.getMinutes()    
  },
  onPullDownRefresh: function(){
    this.getCurSelectNews(function(){
        wx.stopPullDownRefresh()
    })
  } ,
  hotTap: function(){
    let id = this.data.newsId
    wx.navigateTo({
      url: '/pages/detail/index?id=' + id,
    })
  },
  contentTap: function(tap){
    
    let id = this.data.newList[tap.currentTarget.dataset.index]["id"]
    wx.navigateTo({
      url: '/pages/detail/index?id=' + id,
    })
  }
})